package com.suntianci.stock.listener;


import com.github.benmanes.caffeine.cache.Cache;
import com.suntianci.stock.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName StockMQMsgListener
 * @Author 孙天赐
 * @Date 2025/1/5 14:44
 * @Description TODO: 定义股票相关MQ消息监听
 */
@Component
@Slf4j
public class StockMQMsgListener {

    @Autowired
    private Cache<String, Object> caffeineCache;

    @Autowired
    private StockService stockService;

    /**
     * 监听innerMarketQueue队列的消息，刷新大盘数据
     * @param stratTime
     */
    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date stratTime) {
        // 统计当前时间与发送消息时时间的插值，如果超过一分钟，则告警
        // 获取时间毫秒插值
        Long diffTime = DateTime.now().getMillis() - new DateTime(stratTime).getMillis();
        if (diffTime > 60 * 1000L) {
            log.error("大盘发送消息时间:{}, 延迟:{}ms", new DateTime(stratTime).toString("yyyy-MM-dd HH:mm:ss"), diffTime);
        }
        // 刷新缓存
        // 剔除旧数据
        caffeineCache.invalidate("innerMarketKey");
        // 调用服务方法刷新数据
        stockService.getInnerMarketInfo();

    }
}
